import 'package:dio/dio.dart';

//  void _juejin() {
//     var param = {'hello':'123'};
//      DioUtils.requestHttp(
//       'login',
//       parameters: param,
//       method: DioUtils.POST,
//       onSuccess: (data) {
//         print(data);
//       },
//       onError: (error) {},
//     );
//   }
const baseUrl = 'http://10.0.2.2/';
const servicePath = {
  'homePageContext': baseUrl + 'getHomePageContent', // 商家首页信息
  'homePageBelowConten': baseUrl + 'getHomePageBelowConten', //商城首页热卖商品拉取
};

getHomePageContent() {
  return _request('homePageContext');
}

pageBelowConten(page) {
  return _request('homePageBelowConten', formData: {'page': page});
}

// Future getHomePageContent() async {
//   Response response;
//   Dio dio = Dio();
//   response = await dio.post(baseUrl + 'getHomePageContent');
//   return response;
// }

Future _request(url, {formData}) async {
  try {
    print('开始获取数据...............');
    Response response;
    Dio dio = new Dio();
    dio.options.connectTimeout=5000;
    if (formData == null) {
      response = await dio.post(servicePath[url]);
    } else {
      print('formData:$formData');
      response = await dio.post(servicePath[url], data: formData);
    }
    if (response.statusCode == 200) {
      return response;
    } else {
      throw Exception('后端接口出现异常，请检测代码和服务器情况.........');
    }
  } catch (e) {
    return print('ERROR:======>$e');
  }
}
